﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LIMS.Web
{
    public partial class TeachOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindLabIDs();
                bindGradeNos();
            }
        }

        /// <summary>
        /// 绑定班级名称、班级ID
        /// </summary>
        private void bindGradeNos()
        {
            cbClassList.Items.Clear();
            var gradeDict = BLL.StudentManager.GetClassIDAndNo();
            foreach (KeyValuePair<Int32, String> item in gradeDict)
            {
                cbClassList.Items.Add(new ListItem(item.Value, item.Key.ToString()));
            }
            cbClassList.DataBind();
        }

        /// <summary>
        /// 绑定实验室ID
        /// </summary>
        private void bindLabIDs()
        {
            selLabs.DataSource = BLL.LabManager.GetLabIDs().OrderBy(ob => ob.Length);
            selLabs.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            Model.OrderLabs orderLab = new Model.OrderLabs();
            orderLab.labID = selLabs.Value;
            orderLab.orderClassTimeSpan = (Model.OrderClassTimeSpan)Int32.Parse(selLabStatus.Value);
            orderLab.orderTestDate = DateTime.Parse(txtDatePicker.Text.Trim());
            orderLab.orderOperationTime = DateTime.Now;
            orderLab.teacherID = Session["teacherID"].ToString();
            orderLab.testDetail = txtContent.Text.Trim();

            //检测能否被预约
            Boolean isOrdered = BLL.LabOrderManager.CheckOrdered(orderLab);

            //isOrdered为true表示该状态下已经被预约
            if (isOrdered)
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", String.Format("alert('实验室{0}在指定的日期、上课时间段已经被预约！')",selLabs.Value), true);
                return;
            }
            if (BLL.LabOrderManager.AddLabOrderInfo(orderLab))
            {
                String alertURL = String.Format("alert('预约成功!）';windows.location.href='AdmLabOrder.aspx';");
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", alertURL, true);
            }

        }



    }
}